<?php
class Upload {
    public function storeDBData() {
        $sql = "UPDATE
                    menu
                SET
                    Picture = 'menu_pics/" . $this->return . "'
                WHERE
                    Id = " . $this->rootId;
        try {
            $this->db->query($sql);
        } catch (Exception $e) {
            #$this->db->raise_error();
            return false;
        }
    }
    
    public function uploadFile() {
        $file = $this->file;
        if ($this->filename_part) {
            $path = $this->path . "tmp/";
            $file_extension = explode(".", $file['name']);
            $ext = $file_extension[sizeof($file_extension) - 1];
            $filename_part = $this->filename_part;
            $filename = $this->random_key() . "_" . $filename_part . "." . $ext;
        } else {
            $path = $this->path; 
            $filename = $file['name'];
        }
        move_uploaded_file($file['tmp_name'], $path . $filename);
        return $filename;
    }
    
    private function random_key($length = 20) {
        $keys = "abcdefghijklmnopqrstuvwxyz0123456789";

        mt_srand((double)microtime()*1000000);

        for ($i=1; $i <= $length; $i++)
            $random_key .= substr($keys, mt_rand(0,strlen($keys)-1), 1);

        return $random_key;
    }
    
    public function makePicture($width, $height) {
        $path = $this->path;
        $filename = $this->filename;
        $info = getimagesize($path . "tmp/" . $filename);
        $type = pathinfo($path . "tmp/" . $filename);
        if (strtolower($type['extension']) != "jpg" && strtolower($type['extension']) != "jpeg" && strtolower($type['extension']) != "png")
            return "type";
        elseif ($info[0] != $info[1]) {
            return "size";
        } elseif ($info[0] < 191)
            return "size";
        else {
            $from = $path . "tmp/" . $filename;
            $to = $path . $filename;
            $this->thumb($from, $to, $width, $height, TRUE);
            @unlink($from);
            return $filename;
        }
    }
    
    private function thumb($file, $save, $width, $height, $prop) {
    // Requires GD-Lib > 2.0
    // Ist $prop=TRUE, so werden die Proportionen des Bildes
    // auch im Thumbnail eingehalten

    @unlink($save);
    $infos = @getimagesize($file);
    if ($prop) {
        // Proportionen erhalten
        $iWidth = $infos[0];
        $iHeight = $infos[1];
        $iRatio = $width / $iWidth;

        $iNewW = $iWidth * $iRatio;
        $iNewH = $iHeight * $iRatio;

    } else {
        // Strecken und Stauchen auf Gr��e
        $iNewW = $width;
        $iNewH = $height;
    }

    if ($infos[2] == 2) {
        // Bild ist vom Typ jpg
        $imgA = imagecreatefromjpeg($file);
        $imgB = imagecreatetruecolor($iNewW,$iNewH);
        imagecopyresampled($imgB, $imgA, 0, 0, 0, 0, $iNewW,$iNewH, $infos[0], $infos[1]);
        imagejpeg($imgB, $save);
    } elseif ($infos[2] == 3) {
        // Bild ist vom Typ png
        $imgA = imagecreatefrompng($file);
        $imgB = imagecreatetruecolor($iNewW, $iNewH);
        imagecopyresampled($imgB, $imgA, 0, 0, 0, 0, $iNewW,$iNewH, $infos[0], $infos[1]);
        imagepng($imgB, $save);
    } else
        return FALSE;
}
}
?>